<template>
	<comRoot>
		<template #page>
			<comDetailNavigateBack title="宝箱详情"/>
			
			<image v-if="commonConfigs && commonConfigs.domainFrontFileLink" :src="`${commonConfigs.domainFrontFileLink}/files/images/page-bg.png`" class="bg" />
			
			<view class="relative info-data" :style="{'height':`calc(100vh - ${commonNavHeight} )`}">
				<view class="info-list margin-auto">
					<view class="list-content" v-for="(item, index) in ratePoolList" :key="index">
						<view class="list-content-name flex-align-items-center flex-justify-content-between">
							<view class="content-name-box relative flex-align-items-center">
								<image class="bg" v-if="commonConfigs && commonConfigs.domainFrontFileLink" :src="`${commonConfigs.domainFrontFileLink}/files/images/common/level/text-${item.reward_item_type && item.reward_item_type.toLowerCase()}.png`" mode=""></image>
								<image style="height: 38rpx;margin: 0px 40rpx 0px 14rpx;" v-if="commonConfigs && commonConfigs.domainFrontFileLink" :src="`${commonConfigs.domainFrontFileLink}/files/images/common/level/level-${item.reward_item_type && item.reward_item_type.toLowerCase()}.png`" mode="heightFix"></image>
								<text class="relative content-name-txt">获得概率：{{ item.infinite_rate }}</text>
							</view>
							<image class="content-open-icon" v-if="commonConfigs && commonConfigs.domainFrontFileLink" :src="`${commonConfigs.domainFrontFileLink}/files/images/infinite/${item.isOpenUp ? 'ico-open.png' : 'ico-close.png'}`" mode="" @click="setOpenRecordFun(item, index)"></image>
						</view>
						
						<view class="list-content-pool flex" :class="item.isOpenUp ? 'content-pool-openup' : ''">
							<view class="content-pool-detail" v-for="(subItem, subIndex) in item.reward_item" :key="subIndex" @click="gooShopDetailFun(subItem, subIndex, 'boxReward')">
								<view class="content-pool-cover relative flex-all-center">
									<image class="bg absolute" v-if="commonConfigs && commonConfigs.domainFrontFileLink" :src="`${commonConfigs.domainFrontFileLink}/files/images/common/level/small-border-${subItem.reward_item_type && subItem.reward_item_type.toLowerCase()}.png`" mode=""></image>
									<comPlaceImage
										class="pool-cover-img"
										placeImageClass="flex-all-center"
										:lazyLoad="true"
										:src="subItem.reward_item_cover"
										mode="heightFix"></comPlaceImage>
								</view>
								<view class="content-pool-txt" v-if="showNoticeTxtFun(subItem.reward_item_name, 8)" style="margin-top: 10rpx;">
									<uni-notice-bar
										class="uni-notice-bar"
										color="#111111"
										background-color="transparent"
										:scrollable="true"
										:single="true"
										:text="showNoticeTxtFun(subItem.reward_item_name, 8)"
										:speed="54"
									></uni-notice-bar>
								</view>
								<view class="content-pool-txt truncate" v-else>{{ subItem.reward_item_name }}</view>
							</view>
						</view>
					</view>
				</view>
				
			</view>
		</template>
	</comRoot>
</template>

<script>
	import store from '@/store';
	
	import { getContentHomeApi, getRewardBoxListApi  } from '@/setupproject/INTERFACE';
	
	export default {
		name: 'boxDetailPage',
		data() {
			return {
				ratePoolList: [],
			}
		},
		computed: {
			userInfo() {
				return store.getters.userInfo;
			},
			commonNavHeight() {
				return store.getters.commonNavHeight + 'px';
			},
			commonConfigs() {
				return store.getters.commonConfigs || {};
			},
		},
		async onLoad({id}) {
			this.getBoxDetail(id);
		},
		methods: {
			showNoticeTxtFun(name, len) {
				if (name) {
					var namelen = name.length, sum = 0; 
					for (var i = 0; i < namelen; i++) {
						// 中文: 字符编码大于255
						if (name.charCodeAt(i) > 255) {  
							sum += 2;  // 双字节, 累加2
						} else {
							sum++;  // 单字节, 累加1
						}
					}
					if (sum > len*2) {
						return name;
					}
				}
			},
			
			async getBoxDetail(id) {
				const {
					data,
					code,
					msg
				} = await getRewardBoxListApi({
					id, // id	是	int	宝箱ID
				});
			
				if (code !== 200) {
					return uni.showToast({
						icon: 'none',
						title: msg
					});
				}
				
				this.ratePoolList = data.reward_item_index_by_type;
				
				this.ratePoolList.map((item, index) => {
					item.isOpenUp = true;
				});
			},
			
			gooShopDetailFun(item, index, type) {
				if(type === 'reward' && item.reward_item_box_type === 2) {
					return this.openBoxMask(item.reward_item_id, item);
				}
				
				// 宝箱物品详情
				if(type === 'boxReward') {
					this.$store.commit('INFINITE_DETAILS_SET_FUN', item);
					
					return uni.navigateTo({
						url: `/secondary/goodDetailPage/goodDetailPage?type=boxReward`
					});
				}
				
				item.reward_price_1 =  this.rewardData.reward_price_1;
				this.$store.commit('GLOBAL_DETAILS_SET_FUN', item);
				
				uni.navigateTo({
					url: `/secondary/goodDetailPage/goodDetailPage?type=${this.type}`
				});
			},
			
			setOpenRecordFun(item, index) {
				let tempList = JSON.parse(JSON.stringify(this.ratePoolList));
				this.ratePoolList = [];
				tempList[index].isOpenUp = !tempList[index].isOpenUp;
				this.ratePoolList = tempList;
				this.$forceUpdate();
			},
			
			
		}
	}
</script>

<style lang="scss" scoped>
	.info-data {
		font-size: 38rpx;
		color: #fff;
		overflow-y: auto;
		padding-bottom: 70rpx;
		.info-cover {
			width: 707rpx;
			height: 722rpx;
			overflow: hidden;
			.info-cover-img {
				max-width: 408rpx;
				height: 588rpx;
			}
		}
		.info-name {
			margin: 18rpx 0;
			padding: 0 36rpx;
			text-align: center;
			font-size: 40rpx;
			color: #fff;
		}
		
		.info-lever-img {
			image {
				max-width: 200rpx;
				height: 48rpx;
			}
		}
		
		.info-list {
			width: 700rpx;
			margin-top: 24rpx;
			
			.list-content {
				.list-content-name {
					.content-name-box {
						font-size: 26rpx;
						// margin-left: 20rpx;
						width: 395rpx;
						height: 70rpx;
						.content-name-txt {
							// margin-top: 6rpx;
							color: #FFFFFF;
							min-width: 230rpx;
						}
					}
					
					.content-open-icon {
						width: 110rpx;
						height: 44rpx;
					}
				}
				
				.list-content-tip {
					font-size: 20rpx;
					color: #777777;
					
					.content-tip-icon {
						width: 20rpx;
						height: 20rpx;
						margin-right: 6rpx;
					}
				}
				
				.list-content-pool {
					overflow-x: auto;
					margin-top: 16rpx;
					
					&.content-pool-openup {
						flex-wrap: wrap;
					}
					
					.content-pool-detail {
						width: 162rpx;
						margin-right: 10rpx;
						margin-bottom: 22rpx;
						
						.content-pool-cover {
							width: 162rpx;
							height: 190rpx;
							padding-top: 22rpx;
							
							.pool-cover-img {
								width: 132rpx;
								height: 139rpx;
								overflow: hidden;
							}
						}
						
						.content-pool-txt {
							width: 100%;
							height: 26rpx;
							line-height: 26rpx;
							color: #111111;
							font-size: 20rpx;
							text-align: center;
							margin-top: 6rpx;
						}
					}
				}
			}
		}
		
		.info-detail {
			width: 708rpx;
			padding: 38rpx 0rpx;
			.detail-box {
				margin-bottom: 18rpx;
				font-size: 30rpx;
				.detail-title {
					width: 162rpx;
					color: #FFF;
					background: #000;
					border: 2rpx solid #FFAE00;
				}
				.detail-data {
					color: #FFF;
					background: #000;
					border: 2rpx solid #FE7000;
					width: 540rpx;
					min-height: 68rpx;
					padding: 12rpx 8rpx;
					text-align: center;
				}
			}
		}
	}
</style>